<?php
$id = $_GET['id']; settype($id, int); $_SESSION['cauhoi']=$id;
if(@$_POST['btnCancel']) btnCancel($page);
if($id == 0){ //create
	$name = 'btnSubmit';
	$display = 'Thêm mới';
	
	//hidden field user_create
	$form->getProperties(NULL, 'user_create', 2, NULL, $user, 20);
	$user_login = $form->DisplayProperties();
	//hidden field date_create
	$form->getProperties(NULL, 'date_create', 2, NULL, date('Y-m-d H:i:s'), 20);
	$date_create = $form->DisplayProperties();
	$type = 1; //loại sql create $type = 1;
} else {  //update
	$name = 'btnSubmit';
	$display = 'Cập nhật';
	
	$qr_detail = mysql_query("SELECT * FROM {$page} WHERE id='{$id}'");
	$detail = mysql_fetch_array($qr_detail);
	mysql_free_result($qr_detail);
	
	//hidden field user_update
	$form->getProperties(NULL, 'user_update', 2, NULL, $user, 20);
	$user_login = $form->DisplayProperties();
	
	$type = 2; //loại sql update $type = 2;
}

if(!empty($_POST)){
	$table = str_replace('_ac','',$p);
	$field = array_keys($_POST);
	$value = array_values($_POST);
	$sql->get_sql($type,$table,$field,$value,$id);
	$check = $sql->executable();
	if($check == 1) header("location:administrator.php?p=".$page);
	else echo "<p class='error'>{$check}</p>";
}

/*
********************************************************************
* $form->getProperties($displayname, $name, $type, $cssclass, $value = NULL, $length = NULL, $orther = NULL);
* $form->DisplayProperties();
* $type = 1 // text
* $type = 2 // hidden
* $type = 3 // textarea
* $type = 4 // checkbox
* $type = 41// checkbox group
* $type = 5 // radio
* $type = 6 // select
* $type = 7 // button
* $type = 8 // submit
* $type = 9 // DeQuy
********************************************************************
***********
* NOT USED
* other, lang, delete
*/
// form
echo "
<form action='' method='post' name='form1'>
<table width='620' border='0' cellspacing='0' cellpadding='5'>
";
//date create
echo $date_create;
//user login
echo $user_login;

//Trạng thái status
$value = array(1 => 'Hiện', 0 => 'Ẩn');
if($_POST['status'] != '') $check = $_POST['status'];
else if($detail['status'] != '') $check = $detail['status'];
else $check = 1; //giá trị mặc định
$form->getProperties('Trạng thái', 'status', 5, $check, $value, ' &nbsp; ');
echo $form->DisplayProperties();

//Ngày date_update
if(@$_POST['date_update']) $value = $_POST['date_update'];
else if($detail['date_update'] != '') $value = date('d/m/Y', strtotime($detail['date_update']));
else $value = date('d/m/Y');
$form->getProperties('Ngày', 'date_update', 1, 'input_large', $value, 20);
echo $form->DisplayProperties();

//họ tên
if(@$_POST['name']) $value = $_POST['name']; else $value = $detail['name'];
$form->getProperties('Họ tên', 'name', 1, 'input_medium', $value, 100);
echo $form->DisplayProperties();

//email
if(@$_POST['email']) $value = $_POST['email']; else $value = $detail['email'];
$form->getProperties('Email', 'email', 1, 'input_medium', $value, 100);
echo $form->DisplayProperties();

//nội dung
//$length = 2 <=> trộn 2 cột thành 1 cột (colspan='2')
$orther = "
<script>
CKEDITOR.replace( 'content', {
	uiColor: '#b5d8ef',
	toolbar: [
		['Source','-','Cut','Copy','Paste','PasteText','PasteFromWord','Bold','Italic','-','Link','Unlink','TextColor','BGColor'],
	]
});
</script>
";
if(@$_POST['content']) $value = $_POST['content']; else $value = $detail['content'];
$form->getProperties('Nội dung', 'content', 3, 'textarea', $value, 1, $orther);
echo $form->DisplayProperties();

echo "
<tr style='background:#b0b0b0'>
    <th align='right'>&nbsp;</th> 
    <td><input type='submit' name='{$name}' value='{$display}' id='{$name}' class='button' />
	<input type='button' name='btnCancel' value='Hủy' class='button' onClick='window.location.href=\"administrator.php?p={$page}\"'>
	</td>
</tr>
</table>
</form>   <div style='width:600px; padding:10px; margin-top:20px; border:solid 1px #666'>
<div style='width:520px; float:left; cursor:pointer; margin-bottom:5px; color:red; font-weight:bold' class='item_hoidap 0'>Click trả lời</div>
<div style='width:80px; float:left; text-align:right; color:red; font-weight:bold'>Trạng thái</div><div style='clear:both; background-color:#666; height:1px'></div>";
$traloi = mysql_query("SELECT id,name,content,date_update,status FROM answers WHERE question_id={$id} ORDER BY date_update DESC");
while($row_traloi = mysql_fetch_array($traloi)){
	if($row_traloi['status']==0) {$status = 'Ẩn'; $back = 'background-color:#CCC';} else {$status='Bật'; $back = '';}
	echo '<div style="width:520px; float:left; cursor:pointer; padding:5px 0; '.$back.'" class="item_hoidap '.$row_traloi['id'].'"><b>'.$row_traloi['name'].'</b> <em>('.date('d/m/Y', strtotime($row_traloi['date_update'])).')</em>: '.$row_traloi['content'].'</div>';
	echo "<div class='item_status {$row_traloi['id']} {$row_traloi['status']}' style='width:80px; line-height:40px; float:left; text-align:right; color:blue; font-weight:bold; cursor:pointer'>{$status}</div><div style='clear:both; background-color:#666; height:1px'></div>";
}

echo '</div><div style="clear:both; height:50px"></div>';
?>
<style type="text/css">
#backgroundPopup{display:none; position:fixed; _position:absolute; height:100%; width:100%; top:0; left:0; background-color:#000; z-index:1}
#popupContact{display:none; position:fixed; _position:absolute; height:500px; width:auto; z-index:2; font-size:13px; margin:auto}
#popupContact .content{width:450px; float:right; margin-top:50px}
#popupContact h2{text-align:left; color:#FFF; font-size:30px; font-weight:bolder}
#popupContactClose{float:right}
#popupContact p{background: rgba(49,49,49,0.8); padding:10px; color:#FFF; line-height:22px}
</style>
<?php
$ans = mysql_query('SELECT name,email,content FROM answers WHERE id='.$id);
$row_ans = mysql_fetch_array($ans);
?>
<div id="popup"></div>